package com.aoxon.cargo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.GsonBean;
import com.aoxon.cargo.component.MyProgressDialog;
import com.aoxon.cargo.loader.NormalDataLoader;
import com.aoxon.cargo.service.LoginSupplierService;
import com.aoxon.cargo.shared.SupIndexShared;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.CheckStateUtil;
import com.aoxon.cargo.util.EnvironmentUtil;
import com.aoxon.cargo.util.ToastUtil;

/**
 * 登录页面Activity
 * 
 * @author ZZJ
 * 
 */
public class SupLoginActivity extends BaseActivity implements
		NormalDataLoader.OnLoadDataListener {

	private EditText etAccount;
	private EditText etPassword;

	private ImageView ivReturnButton;

	private TextView tvLoginButton;
	private TextView tvRegisterButton;
	private TextView tvForgetPassword;
	private TextView tvTitle;

	private LinearLayout llHolder;

	private MyProgressDialog myProgressDialog;
	private LoginSupplierService loginService = new LoginSupplierService();
	private NormalDataLoader normalDataLoader = new NormalDataLoader(this);
	private GsonBean gsonBean;

	private String strAccount;
	private String strPassword;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setup();

	}


	/**
	 * 自定义方法
	 * ******************************************************************************************************************************************/

	private void setup() {
		setContentView(R.layout.sup_login);

		etAccount = (EditText) findViewById(R.id.etLoginAccount);
		etPassword = (EditText) findViewById(R.id.etLoginPassword);

		ivReturnButton = (ImageView) findViewById(R.id.ivPublicTitleReturn);

		tvTitle = (TextView) findViewById(R.id.tvPublicTiTle);
		tvLoginButton = (TextView) findViewById(R.id.tvLoginButton);
		tvRegisterButton = (TextView) findViewById(R.id.tvRegisterAccount);
		tvForgetPassword = (TextView) findViewById(R.id.tvForgetPassword);
		llHolder = (LinearLayout) etAccount.getParent().getParent();

		tvTitle.setText("拿货助手");
		myProgressDialog = new MyProgressDialog(SupLoginActivity.this);

		normalDataLoader.setOnloadDataListener(this);

		ivReturnButton.setOnClickListener(mClickListener);
		tvLoginButton.setOnClickListener(mClickListener);
		tvRegisterButton.setOnClickListener(mClickListener);
		tvForgetPassword.setOnClickListener(mClickListener);
		llHolder.setOnClickListener(mClickListener);
	}

	/**
	 * 判断帐号密码是否为空
	 * 
	 * @param account
	 * @param passwrod
	 * @return
	 */
	private boolean checkLoginData(String account, String passwrod) {
		if (account.equals("") || account == null) {
			etAccount.requestFocus();
			ToastUtil.show(this, "帐号不能为空");
			return false;
		}
		if (passwrod.equals("") || passwrod == null) {
			etPassword.requestFocus();
			ToastUtil.show(this, "密码不能为空");
			return false;
		}
		return true;
	}

	/**
	 * 提交登录资料
	 */
	private void commitLoginData() {
		strAccount = etAccount.getText().toString();
		strPassword = etPassword.getText().toString();

		if (checkLoginData(strAccount, strPassword)) {
			normalDataLoader.load();
		}
	}

	/**
	 * 提交登录数据返回的操作
	 */
	private void commitResult() {
		try {
			if (CheckStateUtil.check(SupLoginActivity.this, gsonBean, "登录")) {
				// 保存登录的供应商用户的数据
				String strSupplier = gsonBean.getStrJson();
				SupIndexShared.saveSupplierAndKey(strSupplier);
				Intent intent = new Intent(SupLoginActivity.this,
						SupIndexActivity2.class);
				startActivity(intent);
				SupLoginActivity.this.finish();
			}
		} catch (Exception e) {
			e.printStackTrace();
			ToastUtil.show(getBaseContext(), "网络连接异常");
		}
	}

	/**
	 * 系统按键的设置
	 * *****************************************************************************************************************************************/

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			CargoActivity.OPEN_CHOOSE = MyStateUtil.OPEN_MORE;
			Intent intent = new Intent(getBaseContext(), CargoActivity.class);
			startActivity(intent);
			this.finish();
			return false;
		}
		return false;
	}

	/**
	 * 设置各控件的点击监听
	 */
	OnClickListener mClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			Intent intent = null;
			EnvironmentUtil.closeInputMethod(SupLoginActivity.this);

			switch (v.getId()) {
			case R.id.ivPublicTitleReturn:
				CargoActivity.OPEN_CHOOSE = MyStateUtil.OPEN_MORE;
				intent = new Intent(getBaseContext(), CargoActivity.class);
				startActivity(intent);
				SupLoginActivity.this.finish();
				break;

			case R.id.tvLoginButton:
				commitLoginData();
				break;

			case R.id.tvRegisterAccount:
				intent = new Intent(SupLoginActivity.this,
						SupRegisterFastActivity.class);
				startActivity(intent);
				SupLoginActivity.this.finish();
				break;

			case R.id.tvForgetPassword:
				intent = new Intent(SupLoginActivity.this,
						SupForgetPasswordActivity.class);
				startActivity(intent);
				SupLoginActivity.this.finish();
				break;

			default:
				break;
			}
		}
	};

	@Override
	public void beforeLoad() {
		// TODO Auto-generated method stub
		myProgressDialog.show("提示", "登录中...");
	}

	@Override
	public void toLoad(Message msg) throws Exception {
		// TODO Auto-generated method stub
		gsonBean = loginService.execute(strAccount, strPassword);
		msg.what = MyStateUtil.LOGIN_RESULT;
	}

	@Override
	public void afterLoad(int state) {
		// TODO Auto-generated method stub
		myProgressDialog.cancle();
		switch (state) {
		case MyStateUtil.LOGIN_RESULT:
			commitResult();
			break;
		case MyStateUtil.NOT_NET_CONNECTION:
			ToastUtil.show(getBaseContext(), "无网络连接");
			break;
		default:
			break;
		}
	}

}
